package com.evry.spring3sandbox.web;

import java.math.BigDecimal;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.evry.spring3sandbox.service.PriceIncrease;
import com.evry.spring3sandbox.service.ProductManager;

@Controller
public class PriceIncreaseFormController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	@Autowired
	private ProductManager productManager;
//	@Autowired
//	private PriceIncreaseValidation priceIncreaseValidation;

	@RequestMapping(value = "/priceincrease.htm")
	public void showForm(Model model) {
		PriceIncrease priceIncrease = new PriceIncrease();
		priceIncrease.setPercentage(new BigDecimal(20));
		model.addAttribute("priceIncrease", priceIncrease);
	}

	@RequestMapping(value = "/priceincrease.htm", method = RequestMethod.POST)
	public String processForm(@Valid PriceIncrease priceIncrease,
			BindingResult result, Model model) {
		//priceIncreaseValidation.validate(priceIncrease, result);
		if (result.hasErrors()) {
			return "priceincrease";
		}
		productManager.increasePrice(priceIncrease.getPercentage());
		return "redirect:/";
	}
}
